package com.shenmazong.serverwebpoem.task;

import com.shenmazong.serverwebpoem.service.ElasticSearchService;
import com.shenmazong.serverwebpoem.service.TbPoetryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/27 8:44 AM
 * http://www.shenmazong.com
 */

@Slf4j
@Component
@EnableScheduling
public class TaskSavePoetries {

    @Autowired
    TaskThreadService taskThreadService;

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(50);
        return taskScheduler;
    }

    @Value("${com.shenmazong.task.taskSave}")
    private boolean taskSave;

    @Value("${com.shenmazong.task.taskMakeQuestion}")
    private boolean taskMakeQuestion;

    @Value("${com.shenmazong.task.taskGameRoom}")
    private boolean taskGameRoom;

    /**
     * TODO 诗词导入ES线程
     */
    @Scheduled(fixedRate = 1000*60)
    public void saveAllPoetries() {
        log.info("saveAllPoetries ...");
        if(taskSave) {
            taskThreadService.startThreadSavePoeties();
            taskSave = false;
        }
    }

    /**
     * TODO 生成问题线程
     */
    @Scheduled(fixedRate = 1000*30)
    public void makeQuestions() {
        log.info("makeQuestions ...");
        if(taskMakeQuestion) {
            taskThreadService.startThreadMakeQuestions();
            taskMakeQuestion = false;
        }
    }

    @Scheduled(fixedRate = 1000*20)
    public void startGameRoom() {
        log.info("startGameRoom ...");
        if(taskGameRoom) {
            taskGameRoom = false;
            taskThreadService.startThreadGameRoom();
        }
    }
}
